package server.user.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.StrUtil;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/t/user")
public class UserController {

    @PostMapping("change/pass")
    public SaResult passBack(@RequestParam(required = false) String oldPass, @RequestParam(required = false) String newPass) {
        if (StrUtil.isBlank(oldPass)) {
        }
        if (StrUtil.isBlank(newPass)) {
            return SaResult.error("新密码不能为空");
        }
        return SaResult.ok();
    }

    /**
     * 初始化用户密码
     */
    @PostMapping("pass/init/{userId}")
    public SaResult passInit(@PathVariable(required = false) Long userId) {
        if (userId == null) {
            return SaResult.error("参数不能为空");
        }
        return SaResult.ok();
    }


    @GetMapping("getUserInfo")
    public SaResult getUserInfo() {
        long userId = StpUtil.getLoginIdAsLong();
        Map map = new HashMap() {{
            put("id", userId);
            put("name", "Tom");
            put("sex", "女");
            put("age", 18);
        }};
        return SaResult.ok().setData(map);
    }

    /*一般不用专门写这种判断是否已登录的接口*/
    @GetMapping("isLogin")
    public SaResult isLogin() {
        boolean isLogin = StpUtil.isLogin();
        if (isLogin) {
            return SaResult.ok();
        }
        return SaResult.error();
    }
}
